/**
 * 分组管理页面js
 * 版权所有：上海戈品投资管理有限公司
 *   author king
     功能说明：商户管理页面
     创建日期：2015-11-12
     修改记录：
     备注说明：
 */

$(document).ready(function() {
	groupList();
});

/**
 * 查询分组列表
 * @returns
 */
var groupList = function(){
	var datas = {"p":"groupManage","m":"findGroupListPaged" };//请求参数
	var columnDefine = {"id":"ID","name":"名称","type":"类型"};//表格栏目定义
	var formatters = {};//自定义表格格式
	var buttons =''; //操作按钮
	buttons+='<p>';
	buttons+='<button type=\'button\' class=\'btn btn-w-m btn-info\' onclick="showModifyWin();">编辑</button>&nbsp;&nbsp;';
	buttons+='<button type=\'button\' class=\'btn btn-w-m btn-primary\' onclick="showGroupUsers();">查看所属快递员</button>&nbsp;&nbsp;';
	buttons+='<button type=\'button\' class=\'btn btn-w-m btn-danger\' onclick="deleteGroup();">删除</button>';
	buttons+='</p>';
	
	hui.pager('service',datas ,"dataList", columnDefine , formatters , buttons ); //分页查询
};
	

/**
 * 查看组快递员
 * @returns
 */
var showGroupUsers = function(){
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	window.location.href='page?p=page/groupUserList&groupId='+json.id;
};

/**
 * 打开新建窗口
 * @returns
 */
var showNewWin = function(){
	$('#newGroupPop input').val('');
	$('#newGroupPop textarea').val('');
	$('#newGroupPop').modal('show');
};

/**
 * 打开编辑窗口
 * @returns
 */
var showModifyWin = function(obj){
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	$('#modifyGroupPop .id').val(json.id);
	$('#modifyGroupPop .name').val(json.name);
	$('#modifyGroupPop .type').val(json.type);
	$('#modifyGroupPop .remark').val(json.remark);
	$('#pop_win').modal('hide');
	$('#modifyGroupPop').modal('show');
	
};

/**
 * 新建商户
 * @returns
 */
var newGroup = function(){
	
	var name = $('#newGroupPop .name').val();
	var type = $('#newGroupPop .type').val();
	var remark = $('#newGroupPop .remark').val();
	var datas = {"p":"groupManage","m":"addGroup","name":name,"type":type,"remark":remark};
	var json = common.ajaxRequest('service', datas , 'POST');
	if( json && json.result == 0 )
	{
		groupList();
		$('#newGroupPop').modal('hide');
		common.showMsg('info', '新建成功', '新建成功!');
	}
	else
	{
		common.showMsg('error', '创建失败', json.message);
	}	
};

/**
 * 编辑分组
 * @returns
 */
var modifyGroup = function(){
	
	var id = $('#modifyGroupPop .id').val();
	var name = $('#modifyGroupPop .name').val();
	var type = $('#modifyGroupPop .type').val();
	var remark = $('#modifyGroupPop .remark').val();
	
	var datas = {"p":"groupManage","m":"modifyGroup","name":name,"type":type,"remark":remark,"id":id};
	var json = common.ajaxRequest('service', datas , 'POST');
	if( json && json.result == 0 )
	{
		groupList();
		$('#modifyGroupPop').modal('hide');
		common.showMsg('info', '', '修改成功');
	}
	else
	{
		common.showMsg('error', '修改成功', json.message);
	}		
};


/**
 * 删除分组
 * @returns
 */
var deleteGroup = function(){
	
	var data = $('#pop_win .data').text();
	var json = eval('('+data+')');
	
	var datas = {"p":"groupManage","m":"deleteGroup","id":json.id};
	var json = common.ajaxRequest('service', datas , 'POST');
	if( json && json.result == 0 )
	{
		groupList();
		$('#pop_win').modal('hide');
		common.showMsg('info', '', '删除成功');
	}
	else
	{
		common.showMsg('error', '删除成功', json.message);
	}		
};
